export type ITreeData = {
    value:string, 
    title:string,
    children?: ITreeData
}[]

export function getTreeData({data, parentKey, list, isType}:{
    list: ITreeData
    parentKey?: string,
    data: Record<string, any>,
    isType?: boolean
}) {
    if (data && typeof data === 'object') {
        if (data.type) {
            if (data.type === 'object') {
                getTreeData({
                    list,
                    parentKey,
                    data: data.properties,
                    isType
                })
            } else if(data.type === 'array') {
                const name = parentKey + '.0';
                const item = {
                    title: '0',
                    value: isType ? name + '-' + data.items?.type : name,
                    children: []
                }
                getTreeData({
                    list: item.children,
                    parentKey: name,
                    data: data.items,
                    isType
                })

                list.push(item)
            }
        } else {
            Object.keys(data).forEach(key => {
                const name = parentKey ? `${parentKey}.${key}` : key
                const item: ITreeData[number] = {
                    title: key,
                    value:  isType ? `${name}-${data[key].type}` : name,
                    children: []
                }
                if (data[key]?.properties || data[key].items) {
                    getTreeData({
                        list: item.children!,
                        parentKey: name,
                        data: data[key],
                        isType
                    })
                }    
                list.push(item);
            })

        }
    }
}